Skip to content

Conversation

@naveen-seth
Copy link
Contributor

Fixes #159768.

When building a named module interface with -fmodules enabled, importing a Clang module from inside the global module fragment causes Clang to crash only on assertion builds.
This fixes the assert and adds a test.

…agment.

Fixes llvm#159768.

When building a named module interface with -fmodules enabled,
importing a Clang module from inside the global module fragment causes
Clang to crash only on assertion builds.
This fixes the assert and adds a test.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules labels Sep 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-modules

Author: Naveen Seth Hanig (naveen-seth)

Changes

Fixes #159768.

When building a named module interface with -fmodules enabled, importing a Clang module from inside the global module fragment causes Clang to crash only on assertion builds.
This fixes the assert and adds a test.


Full diff: https://github.com/llvm/llvm-project/pull/159771.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaModule.cpp (+6-1)
  • (added) clang/test/Modules/named-module-with-fmodules.cppm (+24)
diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index 773bcb225c188..06c6f309ea6d6 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -772,7 +772,12 @@ void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
     Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
         getLangOpts().CurrentModule, DirectiveLoc, false, false);
     (void)ThisModule;
-    assert(ThisModule && "was expecting a module if building one");
+    // For named modules, the current module name is not known while parsing the
+    // global module fragment and lookupModule may return null.
+    assert((getLangOpts().getCompilingModule() ==
+                LangOptionsBase::CMK_ModuleInterface ||
+            ThisModule) &&
+           "was expecting a  module if building a Clang module");
   }
 }
 
diff --git a/clang/test/Modules/named-module-with-fmodules.cppm b/clang/test/Modules/named-module-with-fmodules.cppm
new file mode 100644
index 0000000000000..a4f7fbec3b176
--- /dev/null
+++ b/clang/test/Modules/named-module-with-fmodules.cppm
@@ -0,0 +1,24 @@
+// Checks that Clang modules can be imported from within the global module 
+// fragment of a named module interface unit.
+// Fixes #159768.
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// RUN: %clang -std=c++23 -fmodules -fmodule-map-file=%t/module.modulemap \
+// RUN:   --precompile %t/A.cppm -o %t/A.pcm
+
+//--- module.modulemap
+module foo { header "foo.h" }
+
+//--- foo.h
+// empty
+
+//--- A.cppm
+module;
+#include "foo.h"
+export module A;
+
+export auto A() -> int { return 42; }
+

@cor3ntin cor3ntin requested a review from ChuanqiXu9 September 19, 2025 13:07
@naveen-seth naveen-seth enabled auto-merge (squash) September 20, 2025 00:01
@naveen-seth naveen-seth merged commit 4554cdf into llvm:main Sep 20, 2025
9 checks passed
pranavk added a commit that referenced this pull request Sep 22, 2025
Otherwise test may fail on some systems.

Fixes tests after #159771
@naveen-seth naveen-seth deleted the clang-module-import-from-gmf branch October 20, 2025 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang][modules] Assertion in Sema::BuildModuleInclude when compiling named module interface with -fmodules.

4 participants